
#include	"SysConfig.h"
#include	"Define.h"
#include	"Port.h"
#include	"Tuner.h"
#include	"Boot_LC786.h"
#include	"Boot_Util.h"
#include	"Model_Option.h"
#include	"Power.h"
#ifdef  CD
#include	"CD_MECHA.h"
#endif
#ifdef  ST7565P
#include	"ST7565P.h"
#endif
#ifdef  HERO
#include	"Hero_Drv.h"
#endif
#ifdef  ATOMIC
#include	"Atomic_DRV.h"
#endif
#ifdef  AV_CONTROLLER
#include	"AVController.h"
#endif
#ifdef  MIC
#include	"MIC.h"
#endif
#ifdef  AUTO_GUIDE
#include	"AutoGuide.h"
#endif

unsigned char ucTask_Timer5msec;
unsigned char ucTaskNo;
#ifdef	LC786XXX
extern void LC786XXX_StartInitialize(void);
extern void LC786XXX_Main(void);
extern void DRV_LC786XXX_CommunicationMain(void);
#endif
#ifdef  BLUETOOTH
extern unsigned char ucBT_InitCold;
extern void BT_BluetoothVariableInitialize(void);
extern void BT_ProcessMain(void);
extern void BT_InitializeStart(void);
#ifdef  MOVON
extern void MV_UartRX_Main(void);
extern void MV_UartTX_Main(void);
#endif
#endif
#ifdef	PT6524
extern void PT6524_UpdateDisplay(void);
extern void PT6524_Initialize(void);
#endif
#ifdef  ST7565P
extern unsigned char ucST7565P_Seq;
extern void ST7565P_Main(void);
#endif
#ifdef  CD
extern void DECK_main(void);
#endif
#ifdef	SERIAL_FLASH
extern unsigned char ucSF_Sequence;
extern void SF_Main(void);
extern unsigned char ucSF_ColdStart;
#endif
#ifdef  PHONE_BOOK
extern void PBAP_Main(void);
#endif

extern void RANDOM_CountRandomSeed(void);
extern void FUNC_Main(void);
extern void CPU_Initialize(void);
extern void DISP_Main(void);
extern void KEY_ProcessMain(void);
extern void KEY_InputMain(void);
extern void PWR_Main(void);
extern void UI_TIMER_Main(void);
extern void TUNER_VariableInitialize(void);
extern void Variable_Initialize(void);
extern void AUDIO_VariableInitialize(void);
extern void AUDIO_BeepOutStop(void);
extern void PWR_GetACCPort(void);
extern void PWR_GetBackUpPort(void);
extern void RTC_CountUp(void);
extern void AUX_Main(void);
extern void AUX_PreDetect(void);

#ifdef  USER_WATCHDOG
extern void CPU_WatchDogMain(void);
#endif

#ifdef  HERO
extern unsigned char ucHERO_BootSeq;
#endif
#ifdef  ATOMIC
extern unsigned char ucATOMIC_BootSeq;
#endif

extern unsigned short usCPU_ADC_Battery;
extern void Option_Read(void);

void Device_InitializeStart(void)
{
	if  (OPT_GetOption(OPT_MASK_CLOCK) == 1)
	{
		PWR_DisplayDriverOn();
	}
	LC786XXX_StartInitialize();
#ifdef  BLUETOOTH
	BT_InitializeStart();
#endif
#ifdef  HERO
	ucHERO_BootSeq = HERO_BOOT_SEQ_START;
#endif
#ifdef  ATOMIC
	ucATOMIC_BootSeq = ATOMIC_SEQ_START;
#endif
#ifdef	SERIAL_FLASH
	ucSF_Sequence = 0;
#endif
//	TUNER_SetPowerOn();
}

#ifdef  DEBUG_PORT

unsigned char ucDbg_inputActive;
unsigned char ucDbg_Active;
unsigned char ucDbg_InputTimer_5msec;

extern unsigned char	ucTuner_CurrentBand;
extern unsigned short	usTuner_Frequency_Current[UI_TUNER_BAND_NUM];	// 8700 ~ 10800
extern unsigned char	ucTuner_PresetNumber[UI_TUNER_BAND_NUM];
extern unsigned short	usTuner_PresetFrequency[UI_TUNER_BAND_NUM][UI_TUNER_PRESET_NUM];
extern unsigned char	ucTuner_Mode;
extern unsigned char	ucTuner_ModeSeq;

extern void TUNER_FrequencyChange(unsigned char ucDes);
extern void CD_SetEject(unsigned char ucDes);
extern void OPT_ReadOptionMemory(void);

void Debug_PortInput(void)
{
	if  (ucDbg_inputActive == 0)
	{
		ucDbg_inputActive = 1;
		pd3_6 = 0;
	}

	if  (p3_6 == 1)
	{
		if  (ucDbg_Active == 1)
		{
			if   (++ucDbg_InputTimer_5msec > (100/5))
			{
				ucDbg_Active = 0;
				ucDbg_InputTimer_5msec = 0;
			}
		}
	}
	else
	{
		if  (ucDbg_Active == 0)
		{
			if  (++ucDbg_InputTimer_5msec > (100/5))
			{
				ucDbg_Active = 1;
				ucDbg_InputTimer_5msec = 0;
		//		ucTuner_CurrentBand = FM1;
		//		usTuner_Frequency_Current[ucTuner_CurrentBand] = 8910;
		//		FUNC_FunctionChangeStart(FUNC_TUNER,FUNC_POWER_ON_SEQ0);
				CD_SetEject(0);
			}
		}
	}
}
#endif
#pragma SECTION program Main_SEC
int main(void)
{
	unsigned char i;

	asm("fclr I");	/* Interrupt disabled */

	pd2_0 = 1;
	AUDIO_MuteOn();

	CPU_Initialize();

	while(1)
	{
		CPU_ADC_GetBatteryLevel();
		if  (usCPU_ADC_Battery > 639)
		{
			break;
		}
	}
			
	for(i = 0; i < 10; ++i)
	{
		PWR_GetBackUpPort();
		PWR_GetACCPort();		
		CPU_ADC_GetValue();
	}

	OPT_ReadOptionMemory();
	
	PO_ACC = 1;
	PO_STBY = 1;
	Variable_Initialize();
	UI_MEMORY_LoadData();
	TUNER_VariableInitialize();
	AUDIO_VariableInitialize();
	Option_Read();
	
#ifdef  BLUETOOTH
	BT_BluetoothVariableInitialize();
	ucBT_InitCold = 1;
#endif
#ifdef  SERIAL_FLASH
	ucSF_ColdStart = 1;
#endif

	Device_InitializeStart();

	asm("fset I");	/* Interrupt enabled */

/*/////////////////////////////////////////////////////
//	Number of Sub-Task : 8
//	Task Tick : 5 msec
//	Total Sycle Time : 40 msec
/////////////////////////////////////////////////////*/
	
//	PWR_PowerControl(PWR_USB, 1);
	PWR_PowerControl(PWR_CTR1, 1);
	LOG_SYS("MainStart");
///////////////Test//////////////////////////////////////*/
	PO_RT1 = 1;
//	PO_RT2 = 1;
/////////////////////////////////////////////////////*/
//	PO_DRV_MUTE = 1;
//	pd10_2 = 1;
//	p10_2 = 0;
#ifdef  DEBUG_PORT
	pd1_4 = 1;
#endif
	#ifdef  ZYLE_LOGO
	DISP_SetLogo();
	#endif
	while(1)
	{
		while(ucTask_Timer5msec)
		{
			PWR_Main();
		#ifdef  PHONE_BOOK
			PBAP_Main();
		#endif
		#ifdef  DEBUG_UART
			UART6_RX_Main();
		#endif
		};
		ucTask_Timer5msec = 1;
//		PO_DEBUG_TOGGLE;

#if  defined(BLUETOOTH) && defined(MOVON)
		MV_UartRX_Main();
#endif
		AUDIO_BeepOutStop();
		UI_TIMER_Main();
		KEY_InputMain();
	#ifdef  HERO
		HERO_BootOn();
	#endif
	#ifdef  ATOMIC
//		PD_NXP_SCL = 0;
//		PD_NXP_SDA = 0;
		ATOMIC_BootOn();
	#endif
	#ifdef  CD
		DECK_main();
	#endif
	#ifdef  PT6524
		PT6524_UpdateDisplay();
	#endif
		TUNER_Main();
		DRV_LC786XXX_CommunicationMain();
	#ifdef  AV_CONTROLLER
		AVC_RX_ProcessMain();
	#endif
		switch  (ucTaskNo)
		{
	////////////////////////////////////////////
			case 0:
			#ifdef  SERIAL_FLASH
				SF_Main();
			#endif
				FUNC_Main();
			#ifdef  AV_CONTROLLER
				AVC_Main();
			#endif
				AUDIO_TelMute();
				break;
	////////////////////////////////////////////
			case 1:
			#ifdef  LC786XXX
				LC786XXX_Main();
				RANDOM_CountRandomSeed();
			#endif
				KEY_ProcessMain();
				break;
	////////////////////////////////////////////
			case 2:
			#ifdef  BLUETOOTH
				BT_ProcessMain();
			#endif
				AUX_Main();
				RTC_CountUp();				
			#ifdef  AUTO_GUIDE				
				AG_AutoGuideMain();				
			#endif
			#ifdef  MIC
				MIC_Main();
			#endif
				break;
	////////////////////////////////////////////
			case 3:
				DISP_Main();
			#ifdef  ST7565P
				ST7565P_Main();
			#endif
			#ifdef  DEBUG_PORT
				Debug_PortInput();
			#endif
			#ifdef  MOVON
				MV_UartTX_Main();
			#endif
			#ifdef  USER_WATCHDOG
				CPU_WatchDogMain();
			#endif
				break;
		}

		if  (++ucTaskNo > 3)
		{
			ucTaskNo = 0;
		}
	}
}
#pragma SECTION program BOOT_UpgradeMain_SEC
void BOOT_UpgradeMain(void)
{
	static union {
	unsigned char   ub[6];
	unsigned short   uw[ (6 / sizeof (unsigned short)) ];
	}FlashData;
	
	BOOT_Main_memcpy(&FlashData.uw[0],(unsigned short far *)0xEFFF0,5);
	
	if  ( FlashData.ub[0] == 0x76 && FlashData.ub[1] == 0x65 && FlashData.ub[2] == 0x72) 
	{	
	#ifdef  M16C_30PG
		BOOT_Main_memcpy(&FlashData.uw[0],(unsigned short far *)0xC0000,5);
	#else	
		BOOT_Main_memcpy(&FlashData.uw[0],(unsigned short far *)0x80000,5);
	#endif
		if  ( FlashData.ub[0] == 0x76 && FlashData.ub[1] == 0x65 && FlashData.ub[2] == 0x72)
		{
			return;
		}
	}
	while(1)
	{
		BOOT_UpgradeInit();
		BOOT_Timer1msec(2000);
		PO_RT1 = 1;
		BOOT_Update_Start(BOOT_HOST_UPDATE);
	}
}